﻿namespace SendKeys.Forms
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Diagnostics;
	using System.Linq;
	using System.Runtime.InteropServices;
	using System.Windows.Forms;

	public partial class MainForm : Form
	{
		private Dictionary<int, Keys> _registeredKeys;

		public MainForm()
		{
			InitializeComponent();

			this.restoreToolStripMenuItem.Click += new EventHandler(OnFormRestoreFromTray);
			this.notifyIcon.DoubleClick += new EventHandler(OnFormRestoreFromTray);
			this.exitToolStripMenuItem.Click += new EventHandler(OnFormExitClicked);
			this.closeToolStripMenuItem.Click += new EventHandler(OnFormExitClicked);

			_registeredKeys = new Dictionary<int, Keys>();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			int currentKeyId = Guid.NewGuid().GetHashCode();
			uint modifiers = 0;
			Keys key = Keys.Control | Keys.Alt | Keys.X;

			if ((key & Keys.Alt) == Keys.Alt)
				modifiers = modifiers | (int)Win32.HotKey.Codes.MOD_ALT;

			if ((key & Keys.Control) == Keys.Control)
				modifiers = modifiers | (int)Win32.HotKey.Codes.MOD_CONTROL;

			if ((key & Keys.Shift) == Keys.Shift)
				modifiers = modifiers | (int)Win32.HotKey.Codes.MOD_SHIFT;

			Keys keyToSend = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;

			if (Win32.HotKey.RegisterHotKey(this.Handle, currentKeyId, modifiers, (uint)keyToSend))
			{
				_registeredKeys[currentKeyId] = key;
			}
			else
			{
				MessageBox.Show(Marshal.GetLastWin32Error().ToString());
			}

			shortCutControl1.DataBind();
		}

		protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);

			if (m.Msg == (int)Win32.HotKey.Codes.WM_HOTKEY)
			{
				int current = m.WParam.ToInt32();
				if (_registeredKeys[current] != null)
				{
					Process[] process = Process.GetProcessesByName("wmplayer");
					foreach (Process index in process)
					{
						//Play/Pause
						//Win32.Message.SendNotifyMessage(index.MainWindowHandle, (uint)Win32.Message.Codes.WM_COMMAND, (UIntPtr)(0x00014978), IntPtr.Zero);

						//Stop
						//Win32.Message.SendNotifyMessage(index.MainWindowHandle, (uint)Win32.Message.Codes.WM_COMMAND, (UIntPtr)(0x00014979), IntPtr.Zero);

						//FF
						//Win32.Message.SendNotifyMessage(index.MainWindowHandle, (uint)Win32.Message.Codes.WM_COMMAND, (UIntPtr)(0x0001497D), IntPtr.Zero);

						uint msg = ((int)Win32.Commands.APPCOMMAND_MEDIA_PLAY_PAUSE) << 16;
						Win32.Message.SendNotifyMessage(index.MainWindowHandle, (uint)Win32.Message.Codes.WM_APPCOMMAND, UIntPtr.Zero, (IntPtr)(msg));
					}
				}
			}
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);
			Win32.HotKey.UnregisterHotKey(this.Handle, _registeredKeys.ElementAt(0).Key);
		}

		private void OnFormExitClicked(object sender, EventArgs e)
		{
			this.Close();
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);

			if (this.WindowState == FormWindowState.Minimized)
			{
				this.ShowInTaskbar = false;
				this.notifyIcon.ShowBalloonTip(2, "SendKeys", "SendKeys Minimized", ToolTipIcon.Info);
			}
			else if (this.WindowState == FormWindowState.Normal)
			{
				this.ShowInTaskbar = true;
			}
			notifyIcon.Visible = !this.ShowInTaskbar;
		}

		private void OnFormRestoreFromTray(object sender, EventArgs e)
		{
			this.Show();
			this.WindowState = FormWindowState.Normal;
		}
	}
}
